package pl.edu.agh.iosr.gamblingzone.adminpanel.action;

import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucher;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateful
@Scope(ScopeType.SESSION)
@Name("availableVouchersManage")
public class AvailableVouchersViewAction implements AvailableVouchersView {
	
	@PersistenceContext
	private EntityManager em;

	@In(required = true)
	DAOFactory dao;
	
	private MoneyVoucherDAO voucherDao;

	@Logger
	Log log;
	
	@In
	private FacesMessages facesMessages;
	
	@DataModel
	private List<MoneyVoucher> availableVouchers;
	
	@Create
	public void init() {
		voucherDao = dao.getVoucherDAO();
	}
	
	public List<MoneyVoucher> show() {
		availableVouchers = voucherDao.findAll();
		log.info(availableVouchers.size());
		
		return null;
	}
	
	public void decline(MoneyVoucher voucher) {
		voucher = em.merge(voucher);
		voucherDao.makeTransient(voucher);
		
		facesMessages.add("Voucher was declined, click button to refresh");
	}
	
	@Remove
	public void destroy() {
	}
}
